package logic

import (
	"context"
	"fmt"
	"taobao-merchandise/models/mysql"

	"taobao-merchandise/taobaoRpc/internal/svc"
	"taobao-merchandise/taobaoRpc/taobaoRpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginUserLogic {
	return &LoginUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginUserLogic) LoginUser(in *taobaoRpc.LoginUserRequest) (*taobaoRpc.LoginUserResponse, error) {
	// todo: add your logic here and delete this line

	user := mysql.User{}
	mysql.DB.Where("account=?", in.Account).Find(&user)

	if user.ID == 0 {
		return nil, fmt.Errorf("用户不存在！")
	}

	if in.Password != user.Password {
		return nil, fmt.Errorf("密码错误！")
	}

	return &taobaoRpc.LoginUserResponse{

		Id: int64(user.ID),
	}, nil
}
